
package predictionEngine;

import java.awt.BorderLayout;

import java.util.List;

import javax.swing.JFileChooser;

import org.jfree.chart.ChartPanel;

/**
 *
 * @author rik
 */
public class GUI extends javax.swing.JFrame {
    private Dataset dataset,trainset,testset;
    private RandomForest forest;
    private ChartPanel chart;
    /** Creates new form GUI */
    public GUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpnlButtons = new javax.swing.JPanel();
        jbtnLoadFile = new javax.swing.JButton();
        jbtnForecast = new javax.swing.JButton();
        jftxfTestsetSize = new javax.swing.JFormattedTextField();
        jLabel1 = new javax.swing.JLabel();
        jbtnEvalAttrbitue = new javax.swing.JButton();
        jcmbBoxAttrbiutes = new javax.swing.JComboBox();
        jpnlChart = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(800, 600));

        jbtnLoadFile.setText("Load File");
        jbtnLoadFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLoadFileActionPerformed(evt);
            }
        });

        jbtnForecast.setText("Forecast");
        jbtnForecast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnForecastActionPerformed(evt);
            }
        });

        jftxfTestsetSize.setText("0.1");

        jLabel1.setText("Testset % size");

        jbtnEvalAttrbitue.setText("Evaluate Attribute");
        jbtnEvalAttrbitue.setActionCommand("jbtnAttributEval");
        jbtnEvalAttrbitue.setEnabled(false);
        jbtnEvalAttrbitue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnEvalAttrbitueActionPerformed(evt);
            }
        });

        jcmbBoxAttrbiutes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Load file" }));
        jcmbBoxAttrbiutes.setEnabled(false);

        javax.swing.GroupLayout jpnlButtonsLayout = new javax.swing.GroupLayout(jpnlButtons);
        jpnlButtons.setLayout(jpnlButtonsLayout);
        jpnlButtonsLayout.setHorizontalGroup(
            jpnlButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnlButtonsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnlButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jbtnLoadFile, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtnForecast, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jftxfTestsetSize, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnEvalAttrbitue)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jcmbBoxAttrbiutes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(344, Short.MAX_VALUE))
        );
        jpnlButtonsLayout.setVerticalGroup(
            jpnlButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnlButtonsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbtnLoadFile)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpnlButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jftxfTestsetSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jbtnForecast)
                    .addComponent(jbtnEvalAttrbitue)
                    .addComponent(jcmbBoxAttrbiutes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jpnlButtons, java.awt.BorderLayout.PAGE_START);

        jpnlChart.setLayout(new java.awt.BorderLayout());
        getContentPane().add(jpnlChart, java.awt.BorderLayout.CENTER);

        setBounds(0, 0, 800, 600);
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnForecastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnForecastActionPerformed
       
        if(dataset!=null){
            forest = new RandomForest(500);
            Dataset[] sets = dataset.split(Double.parseDouble(jftxfTestsetSize.getText()));
            trainset = sets[0];
            testset =sets[1];
            forest.createForest(trainset);
            if(chart!=null)
                jpnlChart.remove(chart);
            chart = LineChartFactory.getInstance().makePredictionChart(trainset,testset,forest);
            jpnlChart.add(chart,BorderLayout.CENTER);
            jbtnEvalAttrbitue.setEnabled(true);
            this.validate();
        }
        
    }//GEN-LAST:event_jbtnForecastActionPerformed

    private void jbtnLoadFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLoadFileActionPerformed
        JFileChooser fc = new JFileChooser();
               fc.showOpenDialog(this);
               ArffReader ar = new ArffReader(fc.getSelectedFile().getAbsoluteFile().toString());
        ar.loadData();
        dataset = new Dataset(ar.getInstances(),ar.getAttributes());
        List<Attribute> atts = ar.getAttributes();
        jcmbBoxAttrbiutes.removeAllItems();
        for(int i=0;i<atts.size()-1;i++)
            jcmbBoxAttrbiutes.addItem(atts.get(i).getName());
        
        if(chart!=null)
            jpnlChart.remove(chart);
        chart = LineChartFactory.getInstance().makeTargetChart(dataset);
        jpnlChart.add(chart,BorderLayout.CENTER);
        jbtnEvalAttrbitue.setEnabled(false);
        jcmbBoxAttrbiutes.setEnabled(true);
        this.validate();
    }//GEN-LAST:event_jbtnLoadFileActionPerformed

    private void jbtnEvalAttrbitueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnEvalAttrbitueActionPerformed
        if(chart!=null)
            jpnlChart.remove(chart);
        chart = LineChartFactory.getInstance().makeAttEvalChart(dataset,forest,jcmbBoxAttrbiutes.getSelectedIndex());
        jpnlChart.add(chart,BorderLayout.CENTER);
        this.validate();
    }//GEN-LAST:event_jbtnEvalAttrbitueActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton jbtnEvalAttrbitue;
    private javax.swing.JButton jbtnForecast;
    private javax.swing.JButton jbtnLoadFile;
    private javax.swing.JComboBox jcmbBoxAttrbiutes;
    private javax.swing.JFormattedTextField jftxfTestsetSize;
    private javax.swing.JPanel jpnlButtons;
    private javax.swing.JPanel jpnlChart;
    // End of variables declaration//GEN-END:variables

}
